// pages/charge/charge.js
import { Agent } from '../agent/agent-model.js'
import { My } from '../my/my-model.js'
var agent = new Agent();
var my = new My();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		index: 0,
		loadingHidden: false,
		availableIncome: 0,
		allowChage: true,
		hasValue: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this._loadData();
	},

	_loadData:function(){
		agent.getAgentBankCard((data) => {
			var bankPicker = [];
			data.forEach(function (value, index) {
				bankPicker[index] = '尾号：' + value.bank_end + '---所有者：' + value.name
			})
			this.setData({
				bankCardId: data[0].bank_card_id,
				bankCards: data,
				bankPicker: bankPicker,
				loadingHidden: true
			})
		})
		my.getUser((data) => {
			var money = parseFloat(data.available_income);
			var am = 0;
			if (money > 1000) {
				var am = Math.floor((money / 1.001) * 100) / 100;
			} else {
				var am = money - 1;
			}
			this.setData({
				inconme: money,
				availableIncome: am,
			})
		});
	},

	picker: function (e) {
		this.setData({
			index: e.detail.value,
			bankCardId: e.currentTarget.dataset.bankcard_id,
		})
	},
	chargeChange: function (e) {
		var money = parseFloat(e.detail.value);
		var allowChage = true;
		var hasValue = false;
		if (money > this.data.availableIncome) {
			allowChage = false;
		}
		if (money > 0) {
			hasValue = true;
		}
		this.setData({
			money: money,
			allowChage: allowChage,
			hasValue: hasValue,
		})
	},
	chargeAll: function () {
		this.setData({
			money: this.data.availableIncome,
			hasValue: true
		})
	},
	chargeBtn: function () {
		if (this.data.hasValue && this.data.allowChage) {
			this.setData({
				hasValue: false,
				btnloading: true
			});

			agent.chargeMoney(this.data.bankCardId, this.data.money, (data) => {
				this.setData({
					hasValue: true,
					btnloading: false
				});
				var that = this;
				if(data.msg){
					wx.showModal({
						title: '提示',
						content: '提现申请成功，预计一个工作日到账',
						complete:function(){
							that._loadData();
						}
					})
				}
			});
		}
	}

})